<?php

namespace Modules\Wechat\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use EasyWeChat\Kernel\Messages\Text;
use EasyWeChat\Kernel\Messages\Image;
use EasyWeChat\Factory;
class WechatController extends Controller
{
    /**
     * name：微信Token配置方法
     */
    public function serve()
    {
        $app = app('wechat.official_account');
		$app->server->push(function ($message) {
			switch ($message['MsgType']) {
				case 'event':
					return '收到事件消息';
					break;
				case 'text':
					return new Text('收到文字消息');
					break;
				case 'image':
					return new Image('zUgJAQN9AyMIMngzCN72kQV8exFIj7FS6lorpKIX-hgMDDRcsSVbN0D3qocc1-LY');
					break;
				case 'voice':
					return '收到语音消息';
					break;
				case 'video':
					return '收到视频消息';
					break;
				case 'location':
					return '收到坐标消息';
					break;
				case 'link':
					return '收到链接消息';
					break;
				case 'file':
					return '收到文件消息';
				// ... 其它消息
				default:
					return '收到其它消息';
					break;
			}
		});
        return $app->server->serve();
    }
	public function index(){
		$app = app('wechat.official_account');
        $res= $app->media->uploadImage(base_path('public').'/upload/image/20190204/blog_user.png'); // $path 为本地文件路径
		dd($res);
	}
	public function user_list(){
		$app = app('wechat.official_account');
		$users = $app->user->list();
		$data = $app->user->select($users['data']['openid']);
		dd($data);
	}
	/**
     * name 网页授权
     */
	public function login(){
		$config  = config('wechat.official_account.default'); 
		$app = Factory::officialAccount($config);
		$oauth = $app->oauth;
		$oauth->redirect()->send();
	} 
	/**
     * name 网页授权回调
     */
	public function oauth_callback(){
		$config  = config('wechat.official_account.default'); 
		$app = Factory::officialAccount($config);
		$user = $app->oauth->user();
		$user = $user['original'];
		unset($user['privilege']);
		$user_url = http_build_query($user,'','&');
		header('location:http://h5.lvacms.cn/#/pages/my/my?'.$user_url);exit();
	}
}
